package org.finalministry.database;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.finalministry.database.dao.AddressDAO;
import org.finalministry.database.dao.CongregationDAO;
import org.finalministry.database.dao.ContactDAO;
import org.finalministry.database.dao.CountryDAO;
import org.finalministry.database.dao.FlagsTagsNotesDAO;
import org.finalministry.database.dao.TerritoryDAO;
import org.finalministry.logger.Logger;
import org.finalministry.logger.LoggerFactory;
import org.finalministry.properties.DatabasePropertiesHolder;

/**
 * Singleton Database Factory used by the DAOs
 * 
 * @author lussopi
 */
public class DatabaseFactory {

	/** The technical logger to use */
	private static final Logger logger = LoggerFactory.getLogger(DatabaseFactory.class);

	// Default is the MotionDriveUnitTests
	private static String persistenceUnitName = "UnitTests";

	private static EntityManagerFactory factory = null;

	private static Long congregationID = null;

	public static synchronized EntityManagerFactory getInstance() {

		if (factory == null) {

			checkForPropertiesFile();

			System.out.println(DatabaseFactory.class.getResource("META-INF/persistence.xml"));

			logger.info("Use persistenceUnitName=" + persistenceUnitName + " for creating entity manager.");
			factory = Persistence.createEntityManagerFactory(persistenceUnitName);

			if (factory == null) {
				System.err.println("No persistence.xml found, I will exit the application!");
				System.exit(0);
			}
		} else {
			logger.warn("EntityManagerFactory already created.");
		}

		return factory;
	}

	/**
	 * check all necessary properties
	 */
	private static void checkForPropertiesFile() {

		DatabasePropertiesHolder propertiesHolder = new DatabasePropertiesHolder();

		if (propertiesHolder.getPersistenceUnitName() != null) {
			persistenceUnitName = propertiesHolder.getPersistenceUnitName();
		}
	}

	public static String getPersistenceUnitName() {

		return persistenceUnitName;
	}

	/**
	 * TODO: call this setter from inside a Initial Class during application
	 * setup
	 * 
	 * @param persistenceUnitName
	 */
	public static void setPersistenceUnitName(String persistenceUnitName) {

		DatabaseFactory.persistenceUnitName = persistenceUnitName;
	}

	public static AddressDAO getAddressDAO() {
		return new AddressDAO();
	}

	public static CongregationDAO getCongregationDAO() {

		return new CongregationDAO();
	}

	public static void setCongregationID(Long congregationID) {
		DatabaseFactory.congregationID = congregationID;
	}

	public static Long getCongregationID() {
		return congregationID;
	}

	public static TerritoryDAO getTerritoryDAO() {

		return new TerritoryDAO();
	}

	public static ContactDAO getContactDAO() {

		return new ContactDAO();
	}

	public static FlagsTagsNotesDAO getFlagsTagsNotesDAO() {

		return new FlagsTagsNotesDAO();
	}

	public static CountryDAO getCountryDAO() {

		return new CountryDAO();
	}

	// public static TransferDAO getTransferDAO() {
	//
	// return new TransferDAO();
	// }

}
